import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { ResultData } from 'src/common/utils/result'
import { RequireRole } from 'src/common/decorators/require-role.decorator'
import { GoodsService } from './goods.service'
import { CreateGoodsDto, UpdateGoodsDto } from './dto/goods.dto'

@ApiTags('商品管理')
@ApiBearerAuth('Authorization')
@Controller('ug/goods')
export class GoodsController {
  constructor(private readonly service: GoodsService) {}

  @ApiOperation({ summary: '商品-分页列表' })
  @RequireRole('admin')
  @Get('list')
  async list(@Query() query: any) {
    const data = await this.service.page(query)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '商品-详情' })
  @RequireRole('admin')
  @Get(':id')
  async detail(@Param('id') id: string) {
    const data = await this.service.findOne(+id)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '商品-新增' })
  @RequireRole('admin')
  @Post()
  async create(@Body() dto: CreateGoodsDto) {
    const data = await this.service.create(dto)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '商品-更新' })
  @RequireRole('admin')
  @Put()
  async update(@Body() dto: UpdateGoodsDto) {
    const data = await this.service.update(dto.goodsId, dto)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '商品-删除' })
  @RequireRole('admin')
  @Delete(':ids')
  async remove(@Param('ids') ids: string) {
    const arr = ids.split(',').map((i) => +i)
    await this.service.remove(arr)
    return ResultData.ok(true)
  }

  @ApiOperation({ summary: '商品-状态变更' })
  @RequireRole('admin')
  @Put('changeStatus')
  async changeStatus(@Body() body: { goodsId: number; status: number }) {
    const data = await this.service.changeStatus(body.goodsId, body.status)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '商品-审核状态变更' })
  @RequireRole('admin')
  @Put('changeAudit')
  async changeAudit(@Body() body: { goodsId: number; auditStatus: number }) {
    const data = await this.service.changeAudit(body.goodsId, body.auditStatus)
    return ResultData.ok(data)
  }

  // ========== SKU 相关 ==========

  @ApiOperation({ summary: 'SKU-列表（按商品）' })
  @RequireRole('admin')
  @Get(':id/skus')
  async listSku(@Param('id') id: string) {
    const data = await this.service.listSku(+id)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: 'SKU-新增' })
  @RequireRole('admin')
  @Post(':id/sku')
  async createSku(@Param('id') id: string, @Body() body: any) {
    const data = await this.service.createSku({ ...body, goodsId: +id })
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: 'SKU-更新' })
  @RequireRole('admin')
  @Put('sku/:skuId')
  async updateSku(@Param('skuId') skuId: string, @Body() body: any) {
    const data = await this.service.updateSku(+skuId, body)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: 'SKU-删除（支持批量）' })
  @RequireRole('admin')
  @Delete('sku/:ids')
  async removeSku(@Param('ids') ids: string) {
    const arr = ids.split(',').map((i) => +i)
    await this.service.removeSku(arr)
    return ResultData.ok(true)
  }

  @ApiOperation({ summary: '库存-变更（正入库/负出库）' })
  @RequireRole('admin')
  @Put('sku/changeStock')
  async changeStock(@Body() body: { skuId: number; change: number; bizType: string; bizNo?: string; remark?: string }) {
    const data = await this.service.changeStock(body)
    return ResultData.ok(data)
  }
}
